package com.api_gateway.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;

@Configuration
public class GatewayRoutesConfig {
    
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
            .route("user-service-auth", r -> r.path("/api/auth/**")
                .filters(f -> f.stripPrefix(1)
                    .retry(config -> config.setRetries(3).setMethods(HttpMethod.GET, HttpMethod.POST)))
                .uri("lb://user-service"))
            
            // 添加视频服务路由
            .route("video-service-publish", r -> r.path("/videos/publish")
                .filters(f -> f.retry(config -> config.setRetries(3).setMethods(HttpMethod.POST)))
                .uri("lb://video-service"))
            
            .route("video-service-api", r -> r.path("/videos/**")
                .filters(f -> f.stripPrefix(1))
                .uri("lb://video-service"))
            
            // 添加点赞服务路由
            .route("interaction-service-likes", r -> r.path("/likes/**")
                .filters(f -> f.stripPrefix(1))
                .uri("lb://demo02-interaction"))
            
            .route("test-httpbin", r -> r.path("/httpbin/**")
                .filters(f -> f.stripPrefix(1))
                .uri("https://httpbin.org"))
            .build();
    }
}